package com.jxb.four;

import com.jxb.tree.TreeNode;

/**
 * 类注释
 *
 * @author jiaobo
 * @date Created in 2025/1/1 17:29
 **/
public class FlattenBTtoLinkedList_114 {

    public static void main(String[] args) {

    }


    public void flattenWithLoop(TreeNode root) {
        while (root != null) {
            if (root.left == null) {
                root = root.right;
            }else {
                //寻找左子树的最右节点
                TreeNode rightmost = root.left;
                while (rightmost.right != null) {
                    rightmost = rightmost.right;
                }
                rightmost.right = root.right;
                root.right = root.left;
                root.left = null;
                root = root.right;
            }
        }
    }

}
